﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using ASPNETVN.PORTAL.Modules.EX.Components;
using Microsoft.Web.Testing.Light;

namespace ASPNETVN.PORTAL.Modules.EX
{
    public class ExchangeRate
    {
        private static Microsoft.Web.Testing.Light.HtmlElement GetRootElement(string url)
        {
            // Create a request for the URL.         
            WebRequest request = WebRequest.Create(url);

            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream();

            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);

            // Read the content.
            string responseFromServer = reader.ReadToEnd();

            // Remove the DOCTYPE.
            responseFromServer = Regex.Replace(responseFromServer, @"\<\!DOCTYPE.*?\>", string.Empty);


            // Cleanup the streams and the response.
            reader.Close();
            dataStream.Close();
            response.Close();

            // Load the response into an HtmlElement.
            return Microsoft.Web.Testing.Light.HtmlElement.Create(responseFromServer);

        }

        public static IList<Item> GetTaxExchangeRate()
        {
            System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("vi-VN");
            string url = "http://www.sbv.gov.vn/vn/CdeQLNH/tygiaXNK.jsp";
            HtmlElement rootElement = GetRootElement(url);

            HtmlElementFindParams tableParam = new HtmlElementFindParams();
            tableParam.TagName = "table";
            tableParam.Attributes.Add("id", "AutoNumber6");
            Microsoft.Web.Testing.Light.HtmlTableElement table = (HtmlTableElement)rootElement.ChildElements.Find(tableParam);
            HtmlElement p = table.PreviousSibling;

            DateTime fromDate = Convert.ToDateTime(p.ChildElements[0].ChildElements[0].CachedInnerTextRecursive.Substring(0, 10), culture.DateTimeFormat);
            DateTime toDate = DateTime.Parse(p.ChildElements[0].ChildElements[1].CachedInnerTextRecursive, culture.DateTimeFormat);

            IList<Item> collection = new List<Item>();
            foreach (HtmlElement row in table.ChildElements)
            {
                try
                {
                    Item item = new Item();
                    item.FromDate = fromDate;
                    item.ToDate = toDate;
                    item.CurrencyCode = row.ChildElements[1].CachedInnerTextRecursive.Trim();
                    item.CurrencyName = row.ChildElements[2].CachedInnerTextRecursive.Trim();
                    item.ExchangeRate = Convert.ToDecimal(row.ChildElements[3].CachedInnerTextRecursive, culture.NumberFormat);

                    collection.Add(item);
                }
                catch
                {

                }
            }
            return collection;
        }

        public static IList<Item> GetUSDExchangeRate()
        {
            System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("vi-VN");
            string url = "http://www.sbv.gov.vn/vn/CdeQLNH/tygia.jsp";
            HtmlElement rootElement = GetRootElement(url);

            HtmlElementFindParams tableParam = new HtmlElementFindParams();
            tableParam.TagName = "table";
            tableParam.Attributes.Add("id", "AutoNumber6");

            Microsoft.Web.Testing.Light.HtmlTableElement table = (HtmlTableElement)rootElement.ChildElements.Find(tableParam);
            HtmlElement font = table.PreviousSibling.PreviousSibling;

            DateTime fromDate = Convert.ToDateTime(font.ChildElements[0].CachedInnerTextRecursive.Substring(0, 10), culture.DateTimeFormat);
            DateTime toDate = Convert.ToDateTime(font.ChildElements[0].CachedInnerTextRecursive.Substring(0, 10), culture.DateTimeFormat);

            IList<Item> collection = new List<Item>();
            try
            {
                Item item = new Item();
                item.FromDate = fromDate;
                item.ToDate = toDate;
                item.CurrencyCode = "USD";
                item.CurrencyName = "Đô la Mỹ";
                item.ExchangeRate = Convert.ToDecimal(table.ChildElements[0].ChildElements[1].ChildElements[1].ChildElements[0].CachedInnerTextRecursive, culture.NumberFormat);

                collection.Add(item);
            }
            catch
            {

            }
            return collection;
        }
    }
}